package com.woniu.liuzhougov.itemAndTask.service.impl;

import com.woniu.liuzhougov.authorityControl.pojo.User;
import com.woniu.liuzhougov.itemAndTask.dao.CheckStatusMapper;
import com.woniu.liuzhougov.itemAndTask.dao.ItemApproveMapper;
import com.woniu.liuzhougov.itemAndTask.dao.ItemsMapper;
import com.woniu.liuzhougov.itemAndTask.pojo.ItemsPojo;
import com.woniu.liuzhougov.itemAndTask.service.ItemApproveService;
import com.woniu.liuzhougov.report.dao.AwaitingApprovalReportMapper;
import com.woniu.liuzhougov.report.pojo.Report;
import com.woniu.liuzhougov.systemSettings.dao.LogMapper;
import com.woniu.liuzhougov.systemSettings.pojo.Log;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional(rollbackFor = Exception.class)
@Service
public class ItemApproveImpl implements ItemApproveService {

    @Autowired
    private ItemApproveMapper itemApproveMapper;
    @Autowired
    private ItemsMapper itemsMapper;
    @Autowired
    private AwaitingApprovalReportMapper mapper;
    @Autowired
    private CheckStatusMapper checkStatusMapper;
    @Autowired
    private LogMapper logMapper;


    /**
     * 添加事项审批结果
     * @param approvalRemark
     * @param approvalResult
     * @param addItemFounderId
     * @param itemId
     * @return
     */
    @Override
    public boolean insertItemApproveResult(String approvalRemark, Integer approvalResult, Integer addItemFounderId, Integer itemId) {
        int count = itemApproveMapper.insertApproveResult(approvalRemark,approvalResult,addItemFounderId,itemId);
        int num = itemsMapper.updateItemStatus(itemId,approvalResult);
        Report report = mapper.selectReportByItemId(itemId);
        Log log = new Log();
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        int resultId = approvalResult;
        String Result = checkStatusMapper.selectByPrimaryKey(resultId).getStatusName();
        log.setLogOperation(Result+"!");
        log.setItemId(itemId);
        log.setUserId(user.getUserId());
        if(report != null){
            //通过 更换 专报状态
            int number = mapper.updateReportState(itemId,approvalResult);
            if(count > 0 && num > 0 && number > 0 ){
                logMapper.insertSelective(log);
                return true;
            }
        }
        if(count > 0 && num > 0){
            logMapper.insertSelective(log);
            return true;
        }
        return false;
    }

    /**
     * 获取 立项人 电话
     * @param itemId
     * @return
     */
    @Override
    public String selectItemFounderPhone(Integer itemId) {
        return itemsMapper.selectFounderPhone(itemId);
    }


    /**
     * 获取事项 内容
     * @param itemId
     * @return
     */
    @Override
    public ItemsPojo selectItemById(Integer itemId) {
        return itemsMapper.searchById(itemId);
    }
}
